package com.czy.daishusys.ui.bottom;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czy.daishusys.R;
import com.czy.daishusys.adapter.PersonPageAdapter;
import com.czy.daishusys.db.UserHelper;
import com.czy.daishusys.db.UserTable;
import com.czy.daishusys.entity.PersonPageItem;
import com.czy.daishusys.global.Constants;
import com.czy.daishusys.ui.BaseFragment;
import com.czy.daishusys.ui.WebActivity;
import com.czy.daishusys.ui.personal.AccountSettingsActivity;
import com.czy.daishusys.utils.MyLog;
import com.czy.daishusys.utils.PhoneUtils;
import com.czy.daishusys.utils.PicDialog;
import com.czy.daishusys.utils.UniversalDialog;
import com.czy.daishusys.utils.glide.GlideCacheUtil;
import com.czy.daishusys.utils.glide.GlideUtils;
import com.czy.daishusys.widget.RecycleViewDivider;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by yang on 2017/11/23.
 * 我的
 */

public class PersonFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;


    private static final String[] TITLE = {"账号设置","清除缓存","关于我们"};
    private static final int[] IMAGE = {R.mipmap.ic_setting_blue_bg,R.mipmap.ic_clean_red_bg,R.mipmap.ic_about_us};

    private ArrayList<PersonPageItem> mDataList = new ArrayList<>();
    private BaseQuickAdapter mAdapter;
    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_person;
    }

    @Override
    protected void initInjector() {
        getToolbarTitle().setText(R.string.person_title);
    }

    @Override
    protected void initViews() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        initData();
        initAdapter();
    }

    private void initData(){
        MyLog.d("PersonFragment","====initData====");
        String[] CONTENT = new String[3];
        CONTENT[0] = UserHelper.getInstance().getUserInfoModel(mContext).getPhone();//第一项：手机号
        CONTENT[1] = "可清除"+GlideCacheUtil.getInstance().getCacheSize();//缓存大小
        CONTENT[2] = "";//空字串
        for(int i = 0 ; i < 3 ; ++i){
            PersonPageItem item = new PersonPageItem();
            item.setName(TITLE[i]);
            item.setIconId(IMAGE[i]);
            item.setContent(CONTENT[i]);
            mDataList.add(item);
        }
    }

    private void initAdapter(){
        mAdapter = new PersonPageAdapter(R.layout.person_page_item_view, mDataList);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position){
                    case 0://账号设置
                        startActivity(new Intent(mContext, AccountSettingsActivity.class));
                        break;
                    case 1://清除缓存
                        cleanCacheDialog("清理缓存", "确定要清理缓存吗?");
                        break;
                    case 2://关于我们
                        String url = Constants.APP_API_BASE + Constants.OWN_ABOUT;
                        WebActivity.navTo(mContext,"关于我们",url);
                        break;
                }
            }
        });

        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void updateViews() {

    }

    private void cleanCacheDialog(String title, String msg) {

        /**有标题，标题图标，信息，取消和确定按钮*/
        PicDialog.Builder builder = new PicDialog.Builder(mContext);
        builder.setTitleIcon(R.mipmap.ic_dialog_clear)
                .setTitle(title)
                .setMessage(msg)
                .setNegativeIcon(R.mipmap.ic_dialog_cancel)
                .setPositiveIcon(R.mipmap.ic_dialog_ensure)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        new cleanCacheAsyncTask().execute();
                    }
                });
        builder.create().show();

    }


    // 清除缓存
    class cleanCacheAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mMProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                GlideUtils.clearDiskCache(getActivity());
            } catch (Exception e) {

            }
            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if(mAdapter != null) {
                mDataList.get(1).setContent("可清除"+GlideCacheUtil.getInstance().getCacheSize());
                mAdapter.notifyDataSetChanged();
                PhoneUtils.ShowToastMessage(getActivity(), "清理成功");
            }
            mMProgressDialog.dismiss();
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        setStatusBar();
        if(mAdapter != null) {
            mDataList.get(1).setContent("可清除"+GlideCacheUtil.getInstance().getCacheSize());
            mAdapter.notifyDataSetChanged();
        }
    }
}
